package com.fas.system.controller;

import com.fas.common.annotation.Log;
import com.fas.common.core.controller.BaseController;
import com.fas.common.core.domain.AjaxResult;
import com.fas.common.core.domain.entity.SysDept;
import com.fas.common.core.page.TableDataInfo;
import com.fas.common.enums.BusinessType;
import com.fas.common.utils.SecurityUtils;
import com.fas.common.utils.poi.ExcelUtil;
import com.fas.system.domain.FasInspectionRecord;
import com.fas.system.domain.vo.EquipmentWordVo;
import com.fas.system.domain.vo.FacilityRecordVo;
import com.fas.system.domain.vo.InspectionRecodeVo;
import com.fas.system.domain.vo.MissedInspectionVo;
import com.fas.system.service.IFasInspectionRecordService;
import com.fas.system.service.ISysDeptService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 巡检记录 Controller
 *
 * @date 2022-07-04
 */
@Api(tags = "巡检记录")
@RestController
@RequestMapping("/system/inspection/record")
public class FasInspectionRecordController extends BaseController {

    @Resource
    private IFasInspectionRecordService fasInspectionRecordService;

    @Resource
    private ISysDeptService deptService;

    @ApiOperation(value = "提交巡检记录")
    @PostMapping("/submit")
    public AjaxResult submitTable(@RequestBody FasInspectionRecord fasInspectionRecord){
        fasInspectionRecord.setInspector(getUserId().intValue());
        return fasInspectionRecordService.submit(fasInspectionRecord);
    }

    /**
     * 小程序巡检员-路线上具体的设施详细数据查询
     * @param fasInspectionRecord
     * @return
     */
    @ApiOperation(value = "小程序巡检员-路线上具体的设施详细数据查询")
    @PostMapping("/findByFacilityAndTaskId")
    public AjaxResult findByFacilityAndTaskId(@RequestBody FasInspectionRecord fasInspectionRecord){
        return AjaxResult.success(fasInspectionRecordService.findByFacilityAndTaskId(fasInspectionRecord));
    }


    /**
     * PC端-巡检记录查询
     * @param inspectionRecodeVo
     * @return
     */
    @ApiOperation(value = "PC端-巡检记录查询")
    @GetMapping("/findAllRecord")
    public TableDataInfo findAllRecord(InspectionRecodeVo inspectionRecodeVo, Integer pageNum, Integer pageSize){
        Long deptId = SecurityUtils.getDeptId();
        SysDept sysDept = deptService.selectDeptById(deptId);
        String deptType = sysDept.getDeptType();
        if (deptType.equals("1") || deptType.equals("2")){
            if(inspectionRecodeVo.getDeptId() == null){
                inspectionRecodeVo.setDeptId(deptId.intValue());
            }
        }
        startPage();
        return getDataTable(fasInspectionRecordService.findAllRecord(inspectionRecodeVo));
    }

    /**
     * PC端-巡检漏检记录查询
     * @param missedInspectionVo
     * @return
     */
    @ApiOperation(value = "PC端-巡检漏检记录查询")
    @GetMapping("/missedInspection")
    public TableDataInfo missedInspection(MissedInspectionVo missedInspectionVo, Integer pageNum, Integer pageSize){
        Long deptId = SecurityUtils.getDeptId();
        SysDept sysDept = deptService.selectDeptById(deptId);
        String deptType = sysDept.getDeptType();
        if (deptType.equals("1") || deptType.equals("2")){
            if(missedInspectionVo.getDeptId() == null){
                missedInspectionVo.setDeptId(deptId.intValue());
            }
        }
        startPage();
        return getDataTable(fasInspectionRecordService.missedInspection(missedInspectionVo));
    }

    /**
     * PC端-巡检记录查询
     * @param inspectionRecodeVo
     * @return
     */
    @ApiOperation(value = "PC端-巡检记录查询-导出")
    @Log(title = "巡检记录", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public void export(HttpServletResponse response, InspectionRecodeVo inspectionRecodeVo){
        Long deptId = SecurityUtils.getDeptId();
        SysDept sysDept = deptService.selectDeptById(deptId);
        String deptType = sysDept.getDeptType();
        if (deptType.equals("1") || deptType.equals("2")){
            if(inspectionRecodeVo.getDeptId() == null){
                inspectionRecodeVo.setDeptId(deptId.intValue());
            }
        }
        List<InspectionRecodeVo> list = fasInspectionRecordService.findAllRecord(inspectionRecodeVo);
        ExcelUtil<InspectionRecodeVo> util = new ExcelUtil<>(InspectionRecodeVo.class);
        util.exportExcel(response, list, "巡检记录数据");
    }

    /**
     * PC端-维修保养-隐患处理-设施
     * @param
     * @return
     */
    @ApiOperation(value = "PC端-维修保养-隐患处理-设施")
    @GetMapping("/findFacilityOrderPage")
    public TableDataInfo findFacilityOrderPage(FacilityRecordVo facilityRecordVo,Integer pageNum,Integer pageSize){
        Long deptId = SecurityUtils.getDeptId();
        SysDept sysDept = deptService.selectDeptById(deptId);
        String deptType = sysDept.getDeptType();
        if (deptType.equals("1") || deptType.equals("2")){
            if(facilityRecordVo.getDeptId() == null){
                facilityRecordVo.setDeptId(deptId.intValue());
            }
        }
        startPage();
        return getDataTable(fasInspectionRecordService.findFacilityOrderPage(facilityRecordVo));
    }

    /**
     * PC端-维修保养-隐患处理-设施-详细
     * @param
     * @return
     */
    @ApiOperation(value = "PC端-维修保养-隐患处理-设施-详细")
    @GetMapping("/findAuditDetails")
    public AjaxResult findAuditDetails(Integer id){
        return AjaxResult.success(fasInspectionRecordService.findAuditDetails(id));
    }

    /**
     * PC端-维修保养-隐患处理-设备
     * @param
     * @return
     */
    @ApiOperation(value = "PC端-维修保养-隐患处理-设备")
    @GetMapping("/findDeviceOrderPage")
    public TableDataInfo findDeviceOrderPage(EquipmentWordVo equipmentWordVo, Integer pageNum, Integer pageSize){
        Long deptId = SecurityUtils.getDeptId();
        SysDept sysDept = deptService.selectDeptById(deptId);
        String deptType = sysDept.getDeptType();
        if (deptType.equals("1") || deptType.equals("2")){
            if(equipmentWordVo.getDeptId() == null){
                equipmentWordVo.setDeptId(deptId.intValue());
            }
        }
        startPage();
        return getDataTable(fasInspectionRecordService.findDeviceOrderPage(equipmentWordVo));
    }


    /**
     * PC端-维修保养-隐患处理-设备-详细
     * @param
     * @return
     */
    @ApiOperation(value = "PC端-维修保养-隐患处理-设备-详细")
    @GetMapping("/findEquipmentDetails/{id}")
    public AjaxResult findEquipmentDetails(@PathVariable("id") Integer id){
        return AjaxResult.success(fasInspectionRecordService.findEquipmentDetails(id));
    }


    /**
     * PC端-维修保养-隐患处理-设备-详细
     * @param
     * @return
     */
    @ApiOperation(value = "PC端-消防巡检-巡检统计-巡检趋势变化图")
    @GetMapping("/trendChange")
    public AjaxResult trendChange(){

        return AjaxResult.success(fasInspectionRecordService.trendChange(getDeptId().intValue()));
    }



}
